Python+获取照片中的GPS信息

用智能手机或数码相机拍摄的照片有丰富的附加信息元数据,通常以 EXIF 格式存储这些元数据,EXIF 即Exchangeable Image File Format,有不同的标准版本。
EXIF 元数据可能包含关于设备模型、尺寸、图片的日期及其位置的信息。

安装exif 库

pip install exif

读取图片

from exif import Image
img_path = 'images/image_exif.jpg'
with open(img_path, 'rb') as src:
    img = Image(src)
    print (src.name, img)

输出内容为:

images/image_exif.jpg <exif._image.Image object at 0x00000223EBC62430>

通过PTL库读取该输出即可获取图像内容:

import PIL
image = PIL.Image.open(img_path)
image

在这里插入图片描述

检查图片中是否含有EXIF数据:

if img.has_exif:
 info = f"has the EXIF {img.exif_version}"
else:
 info = "does not contain any EXIF information"
print(f"Image {src.name}: {info}")

若有,提示:

Image images/image_exif.jpg:  has the EXIF 0220

没有:

# Image without EX
with open('images/foto_no_exif.jpg', "rb") as src:
 img = Image(src)
 if img.has_exif:
    info = f" has the EXIF {img.exif_version}"
 else:
    info = "does not contain any EXIF information"
 print(f"Image {src.name}: {info}")

输出:

Image images/foto_no_exif.jpg: does not contain any EXIF information

获取图像中的EXIF元数据

# Read again photo with exif info
with open(img_path, “rb”) as src:
 img = Image(src)
img.list_all()

该列表显示了元数据中所有的标签内容:

['image_description',
 'make',
 'model',
 'orientation',
 'x_resolution',
 'y_resolution',
 'resolution_unit',
 'software',
 'datetime',
 'y_and_c_positioning',
 '_exif_ifd_pointer',
 '_gps_ifd_pointer',
 'compression',
 'jpeg_interchange_format',
 'jpeg_interchange_format_length',
 'exposure_time',
 'f_number',
 'exposure_program',
 'photographic_sensitivity',
 'exif_version',
 'datetime_original',
 'datetime_digitized',
 'components_configuration',
 'exposure_bias_value',
 'max_aperture_value',
 'metering_mode',
 'light_source',
 'flash',
 'focal_length',
 'maker_note',
 'user_comment',
 'flashpix_version',
 'color_space',
 'pixel_x_dimension',
 'pixel_y_dimension',
 '_interoperability_ifd_Pointer',
 'file_source',
 'scene_type',
 'custom_rendered',
 'exposure_mode',
 'white_balance',
 'digital_zoom_ratio',
 'focal_length_in_35mm_film',
 'scene_capture_type',
 'gain_control',
 'contrast',
 'saturation',
 'sharpness',
 'subject_distance_range',
 'gps_latitude_ref',
 'gps_latitude',
 'gps_longitude_ref',
 'gps_longitude',
 'gps_altitude_ref',
 'gps_timestamp',
 'gps_satellites',
 'gps_img_direction_ref',
 'gps_map_datum',
 'gps_datestamp']

获取图像中的地理坐标

将经纬度坐标转换为十进制数值表示:

def decimal_coords(coords, ref):
 decimal_degrees = coords[0] + coords[1] / 60 + coords[2] / 3600
 if ref == "S" or ref == "W":
     decimal_degrees = -decimal_degrees
 return decimal_degrees

获取十进制经纬度坐标以及其他信息函数:

def image_coordinates(img_path):
    with open(img_path, 'rb') as src:
        img = Image(src)
    if img.has_exif:
        try:
            img.gps_longitude
            coords = (decimal_coords(img.gps_latitude,
                      img.gps_latitude_ref),
                      decimal_coords(img.gps_longitude,
                      img.gps_longitude_ref))
        except AttributeError:
            print 'No Coordinates'
    else:
        print 'The Image has no EXIF information'
    print(f"Image {src.name}, OS Version:{img.get('software', 'Not Known')} ------")
    print(f"Was taken: {img.datetime_original}, and has coordinates:{coords}")

输出内容:

Image images/DSCN0012.jpg, OS Version:Nikon Transfer 1.1 W ------
Was taken: 2008:10:22 16:29:49, and has coordinates:(43.46715666666389, 11.885394999997223)

参考:

https://medium.com/spatial-data-science/how-to-extract-gps-coordinates-from-images-in-python-e66e542af354

  • 3
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!如果您想使用 Pytest、Python 和 Playwright 获取网页信息并进行存储,可以按照以下步骤进行: 1. 安装 Pytest 和 Playwright 库:在命令行输入 `pip install pytest playwright`,即可安装 Pytest 和 Playwright 库。 2. 编写测试用例:使用 Pytest 的 API,可以编写测试用例,包括打开网页、获取元素信息、填写表单、点击按钮等。您可以根据需要编写测试用例,实现获取网页信息的功能。 3. 存储数据:在获取到网页信息后,可以将其存储到本地文件或数据库。您可以使用 Python 提供的文件操作或数据库库(如 MySQLdb)进行存储操作。 下面是一个示例测试用例,用于获取百度搜索结果页面的所有链接,并将其存储到本地文件: ```python import os import pytest from playwright.sync_api import Playwright, sync_playwright @pytest.fixture(scope="module") def playwright(): with sync_playwright() as p: yield p def test_get_links(playwright: Playwright): browser = playwright.chromium.launch() page = browser.new_page() page.goto('https://www.baidu.com/s?wd=playwright') links = page.query_selector_all('a') with open('links.txt', 'w') as f: for link in links: if link.get_attribute('href'): f.write(link.get_attribute('href') + os.linesep) browser.close() ``` 该测试用例使用 Pytest 和 Playwright 打开百度搜索结果页面,获取所有链接,将其写入本地文件 `links.txt` ,并最后关闭浏览器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值